<?php

namespace Mobile\Controller;

use Common\Lib\Upload\ImgManager;
use Common\Model\UserFavoritesModel;
use Common\Lib\Relation;

class BbsController extends CommonController
{
    /**
     * 主题列表页
     */
    public function lists()
    {
        $this->assign('pagemark', 'bbs');
        $this->display();
    }

    /**
     * Ajax主题列表
     */
    public function ajax_bbs_list()
    {
        $p = I('get.p', '1', 'intval');
        $startrow = ($p - 1) * 5;
        $this->assign(compact('startrow'));
        $html = $this->fetch('ajax_bbs_list');
        if (substr($html, -13) == '<!--empty -->') {
            $this->ajaxReturn(0, '没有数据');
        }
        $this->ajaxReturn(1, '', array(
            'html' => $html,
            'loadurl' => query_url('ajax_bbs_list', array('p' => $p + 1), '', true),
        ));
    }

    /**
     * 主题点赞
     */
    public function ajax_bbs_praise()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $qid = I('param.qid', '', 'intval');

        if (empty($qid)) {
            $this->ajaxReturn('参数错误');
        }

        $ret = D('Question')->praise($uid, $qid);
        $data['qid'] = $qid;
        $data['count_str'] = format_praise(D('QuestionPraise')->where(compact('qid'))->count());
        if ($ret['err'] !== 0) {
            $this->ajaxReturn(0, $ret['msg']);
        } else {
            $this->ajaxReturn(1, '点赞成功', $data);
        }
    }

    /**
     * 收藏问题
     */
    public function ajax_bbs_favorites()
    {
        $this->check_login();
        $uid = session('ytata.uid');
        $params = I('param.params');
        $qid = $params['qid'];
        $type = UserFavoritesModel::TYPE_QUESTION;

        if (empty($qid)) {
            $this->ajaxReturn(0, '参数错误');
        }

        $data['qid'] = $qid;
        if (D('UserFavorites')->chenk_favorites($uid, $qid, $type)) {
            D('UserFavorites')->clear_favorites($uid, $qid, $type);
            $this->ajaxReturn(2, '已取消收藏', $data);
        } else {
            D('UserFavorites')->add_favorites($uid, $qid, $type);
            $this->ajaxReturn(1, '收藏成功', $data);
        }
    }

    /**
     * 提交主题页面
     */
    public function ajax_bbs_add()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    /**
     * 提交主题保存
     */
    public function ajax_bbs_save()
    {
        $this->check_login();
        // 读取数据
        $data = I('post.');
        $uid = session('ytata.uid');
        // 保存主题
        $ret = D('Bbs')->bbs_add($data, $uid);
        if (!$ret['status']) {
            $this->ajaxReturn(0, $ret['msg']);
        }
        $this->ajaxReturn(1, $ret['msg']);
    }

    /**
     * 获取标签
     */
    public function ajax_get_tags()
    {
        $page = I('get.p', 0, 'intval');
        $limit = 5;
        $count = D('Tag')->count();
        $maxPage = ceil($count / $limit);
        $page = ($page <= $maxPage) ? $page : 1;
        $tags = D('Tag')->page($page, $limit)->getField('tag', true);
        $this->ajaxReturn(1, '', compact('tags', 'page'));
    }

    /**
     * 文章详细页
     */
    public function view()
    {
        $id = I('get.id', '0', 'intval');

        $question = D('Question')->get_question_one($id);
        if (!$question) {
            die('文章不存在');
        }
        $question['adduser'] = D('User')->get_user_one($question['uid'], true);
        $map['qid'] = $question['id'];
        $sort = 'optimal desc,id desc';
        $num = 10;  // 分页
        $ret = D('Answer')->get_list($map, true, $sort, $num);
        $answers = array();
        if (!empty($ret['list'])) {
            $answers = $ret['list'];
        }

        $this->assign(compact('question', 'answers'));
        $this->assign('pagemark', 'bbs');
        $this->display();
    }

    /**
     * 回复
     */
    public function ajax_bbs_reply()
    {
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    /**
     * 回复保存
     */
    public function ajax_bbs_reply_save()
    {
        $this->check_login();
        $content = I('param.content', '', 'trim');
        $bid = I('param.bid', '', 'intval');
        $uid = session('ytata.uid');

        $data['content'] = $content;
        $ret = D('BbsReply')->reply_add($data, $bid, $uid);

        if (!$ret['status']) {
            $this->ajaxReturn(0, $ret['msg']);
        }
        $this->ajaxReturn(1, $ret['msg']);
    }

    /**
     * 回贴聊天
     */
    public function ajax_bbs_reply_supp()
    {
        $this->check_login();
        $html = $this->fetch();
        $this->ajaxReturn(1, '', $html);
    }

    /**
     * 回帖聊天保存
     */
    public function ajax_bbs_reply_supp_save()
    {
        $this->check_login();

        $rid = I('param.rid', '', 'intval');
        $to_uid = I('param.to_uid', '', 'intval');
        $content = I('param.content', '', 'trim');
        $send_uid = session('ytata.uid');

        $data['content'] = $content;
        $ret = D('BbsReplyChat')->chat_add($data, $rid, $send_uid, $to_uid);
        if (!$ret['status']) {
            $this->ajaxReturn(0, $ret['msg']);
        } else {
            $this->ajaxReturn(1, $ret['msg']);
        }
    }
}